package virtualadvisor;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JComboBox;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.DefaultComboBoxModel;

public class ProfileSelector extends JDialog {

	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private Profile mainProfile;

	/**
	 * Launch the Profile Selector application.
	 */
	public static void main(String[] args) {
		try {
			Profile p = new Profile();
			ProfileSelector dialog = new ProfileSelector(p);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public ProfileSelector(final Profile profile) {
		this.mainProfile = profile;
		setTitle("Virtual Advisor - Plan Selector");
		setBounds(100, 100, 450, 231);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JLabel lblSelectNewPlan = new JLabel("Select New Plan or Load a Plan");
		lblSelectNewPlan.setFont(new Font("Tahoma", Font.BOLD, 18));
		lblSelectNewPlan.setBounds(10, 10, 333, 22);
		contentPanel.add(lblSelectNewPlan);
		
		JLabel lblCreateNewPlan = new JLabel("Create New Plan");
		lblCreateNewPlan.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblCreateNewPlan.setBounds(10, 52, 125, 14);
		contentPanel.add(lblCreateNewPlan);
		
		JButton btnPlanWizard = new JButton("Plan Wizard");
		btnPlanWizard.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				setVisible(false);
				PlanWizard dialog2 = new PlanWizard(profile);
				
				dialog2.setVisible(true);
				dialog2.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			}
		});
		btnPlanWizard.setFont(new Font("Tahoma", Font.PLAIN, 11));
		btnPlanWizard.setBounds(10, 72, 125, 23);
		contentPanel.add(btnPlanWizard);
		
		JLabel lblChooseProfile = new JLabel("Choose Plan");
		lblChooseProfile.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblChooseProfile.setBounds(10, 116, 168, 14);
		contentPanel.add(lblChooseProfile);
		
		String[] pFile = {""};
		
		final JComboBox comboBox_LoadProfile = new JComboBox(pFile);
		Profile xProfile = new Profile();
		comboBox_LoadProfile.setModel(new DefaultComboBoxModel(xProfile.getAllPlanNames()));
		
		comboBox_LoadProfile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		comboBox_LoadProfile.setBounds(10, 136, 405, 22);
		contentPanel.add(comboBox_LoadProfile);

		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
		getContentPane().add(buttonPane, BorderLayout.SOUTH);

		JButton btnLoadPlan = new JButton("Load Plan");
		btnLoadPlan.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String profileSelected = (String) comboBox_LoadProfile.getSelectedItem();
				if(profileSelected.equals("")){
					JOptionPane.showMessageDialog(contentPanel,
						"Please Select a Profile.",
						"Warning", JOptionPane.WARNING_MESSAGE);
				} else {			    
					//System.out.println(profileSelected);
							
					//TODO Add code to load the profile selected, profileSelected 
					//     is the profile to be loaded from file.
					
					setVisible(false);
					dispose();
					MainGUI window = new MainGUI(mainProfile);
					mainProfile.loadPlanXMLFile(profileSelected.trim(), "courseCatalog.xml");
					mainProfile.setCurrentPlanName(profileSelected.trim());
					window.initialize();
					window.frame_MainGui.setVisible(true);
				}
			}
		});
		
		JButton btnDeleteProfile = new JButton("Delete Plan");
		btnDeleteProfile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String profileSelected = (String) comboBox_LoadProfile.getSelectedItem();
				if(profileSelected.equals("")){
					JOptionPane.showMessageDialog(contentPanel,
						"Please Select a Profile.",
						"Warning", JOptionPane.WARNING_MESSAGE);
				} else {			    
					try {
						int warningYesNo =JOptionPane.showConfirmDialog(null, 
								"Are you sure you want to remove \nthis profile?", 
								"Virtual Advisor", JOptionPane.YES_NO_OPTION);
						if (warningYesNo == JOptionPane.YES_OPTION) {
	                        //TODO Add code to remove the profile
	                    } 
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		});
		buttonPane.add(btnDeleteProfile);
		
		btnLoadPlan.setActionCommand("Cancel");
		buttonPane.add(btnLoadPlan);
	}
}

